//
//  ExperienceVC.m
//  HappyStore
//
//  Created by 李小辉 on 16/7/5.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "ExperienceVC.h"
#import "WorkExperience.h"
#import "EducationExperience.h"
#import "eduDatePickView.h"
#import "SingleSelectedDialog.h"
@interface ExperienceVC ()<selectedDateDelegate1,selectedSingleRowDelegate>
@property (strong, nonatomic) WorkExperience *workExperienceView;//工作
@property (strong, nonatomic) EducationExperience *educationView;//教育
@property (strong, nonatomic) NSArray *educationalAry;//学历
@property (assign, nonatomic) BOOL selectStartAndEndDate;
@property (strong, nonatomic) SingleSelectedDialog *educationstry;
@end

@implementation ExperienceVC

- (NSArray *)educationalAry{
    if (!_educationalAry) {
        _educationalAry = @[@"初中",@"高中",@"专科",@"大学",@"硕士",@"博士"];
    }
    return _educationalAry;
}

- (EducationExperience *)educationView{
    if (!_educationView) {
        _educationView = [[[NSBundle mainBundle] loadNibNamed:@"EducationExperience" owner:self options:nil] lastObject];
    }
    return _educationView;
}
- (WorkExperience *)workExperienceView{
    if (!_workExperienceView) {
        _workExperienceView = [[[NSBundle mainBundle] loadNibNamed:@"WorkExperience" owner:self options:nil] lastObject];
    }
    return _workExperienceView;
}
- (NSDictionary *)oldExperienceDic{
    if (!_oldExperienceDic) {
        _oldExperienceDic = [[NSDictionary alloc] init];
    }
    return _oldExperienceDic;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    navBarHairlineImageView.alpha = 0.5f;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    if ([self.tigStr integerValue] == 1 || [self.tigStr integerValue] == 3) {
        titleLabel.text = @"职业经历";
    }else{
        titleLabel.text = @"教育经历";
    }
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(btnClickRight)];
    rightBar.tintColor = RGB(45, 45, 45);
    self.navigationItem.rightBarButtonItem = rightBar;
    //取消显示后退键文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    //监听按钮
    //职业
    [self.workExperienceView.startTimeBtn addTarget:self action:@selector(clickStartTimeBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.workExperienceView.endTimeBtn addTarget:self action:@selector(clickEndTimeBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.workExperienceView.upToNowSwitch addTarget:self action:@selector(upToNowSwitchChanged:) forControlEvents:UIControlEventValueChanged];
    //教育
    [self.educationView.startTimeBtn addTarget:self action:@selector(clickStartTimeBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.educationView.endTimeBtn addTarget:self action:@selector(clickEndTimeBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.educationView.educationalBtn addTarget:self action:@selector(clickEducational) forControlEvents:UIControlEventTouchUpInside];
  
    [self setupView];
}

- (void)setupView{
    //tigStr = 1修改职业经历 2修改教育经历 3添加职业经历 4添加教育经历
    if ([self.tigStr integerValue] == 1) {//修改职业经历
        self.workExperienceView.companyTextField.text = self.oldExperienceDic[@"workName"];
        self.workExperienceView.positionTextField.text = self.oldExperienceDic[@"workPosition"];
        self.workExperienceView.desTextView.text = self.oldExperienceDic[@"workMiaosu"];
        NSArray *timeAry = [self.oldExperienceDic[@"workTime"] componentsSeparatedByString:@"-"];
        if (timeAry) {
            self.workExperienceView.startTime.text = timeAry[0];
            self.workExperienceView.endTime.text = timeAry[1];
        }
        self.workExperienceView.frame = CGRectMake(0, 64, screenW, self.workExperienceView.height);
        [self.view addSubview:self.workExperienceView];
    }else if ([self.tigStr integerValue] == 2){//修改教育经历
        self.educationView.schoolNameTextField.text = self.oldExperienceDic[@"schoolName"];
        self.educationView.professionTextField.text = self.oldExperienceDic[@"schoolStudy"];
        self.educationView.EducationalLabel.text = self.oldExperienceDic[@"schoolIng"];
        self.educationView.EducationaTextView.text = self.oldExperienceDic[@"schoolMiaosu"];
        NSArray *timeAry = [self.oldExperienceDic[@"schoolTime"] componentsSeparatedByString:@"-"];
        if (timeAry) {
            self.educationView.startTimeLabel.text = timeAry[0];
            self.educationView.endTimeLabel.text = timeAry[1];
        }
        self.educationView.frame = CGRectMake(0, 64, screenW, self.educationView.height);
        [self.view addSubview:self.educationView];
    }else if ([self.tigStr integerValue] == 3){//添加职业经历
        self.workExperienceView.frame = CGRectMake(0, 64, self.workExperienceView.width, self.workExperienceView.height);
        [self.view addSubview:self.workExperienceView];
    }else{//添加教育经历
        self.educationView.frame = CGRectMake(0, 64, screenW, self.educationView.height);
        [self.view addSubview:self.educationView];
    }
}
//选择学历
- (void)clickEducational{
    self.educationstry = [SingleSelectedDialog sharedSelectedAddress];
    self.educationstry.delegatee = self;
    NSMutableArray *muAry = [NSMutableArray array];
    for (int i=0; i<self.educationalAry.count; i++) {
        [muAry addObject:self.educationalAry[i]];
    }
    [self.educationstry btnClickAddressWithData:muAry];
}
- (void)seceltedRow:(NSInteger)rrow{
    self.educationView.EducationalLabel.text = self.educationalAry[rrow];
}
//选择开始时间
- (void)clickStartTimeBtn{
    self.selectStartAndEndDate = YES;
    eduDatePickView *datt = [eduDatePickView sharedSelectedAddress];
    datt.delegatee = self;
    [datt btnClickDate];
}
//选择至今按钮
- (void)upToNowSwitchChanged: (UISwitch *)upToNowSwitch{
    if (upToNowSwitch.isOn) {
        self.workExperienceView.endTime.text = @"至今";
        self.workExperienceView.endTimeBtn.userInteractionEnabled = NO;
    }else {
        self.workExperienceView.endTime.text = @"必填";
        self.workExperienceView.endTimeBtn.userInteractionEnabled = YES;
    }
}
//选择结束时间
- (void)clickEndTimeBtn{
    self.selectStartAndEndDate = NO;
    eduDatePickView *datt = [eduDatePickView sharedSelectedAddress];
    datt.delegatee = self;
    [datt btnClickDate];
}
- (void)seceltedDate:(NSString *)date{
    if (self.selectStartAndEndDate) {
        if ([self.tigStr integerValue] == 1 || [self.tigStr integerValue] == 3) {
            self.workExperienceView.startTime.text = date;
        }else{
            self.educationView.startTimeLabel.text = date;
        }
        
    }else {
        if ([self.tigStr integerValue] == 1 || [self.tigStr integerValue] == 3) {
            self.workExperienceView.endTime.text = date;
        }else{
            self.educationView.endTimeLabel.text = date;
        }
    }
}

#pragma mark - 保存按钮
- (void)btnClickRight{
    BOOL isOk = YES;
    if ([self.tigStr integerValue] == 1 ||[self.tigStr integerValue] == 3 ) {
        if (isOk && self.workExperienceView.companyTextField.text.length == 0) {
            isOk = NO;
            [self showSingleIconBubble:@"请输入公司名称"];
        }
        if (isOk && self.workExperienceView.positionTextField.text.length == 0) {
            isOk = NO;
            [self showSingleIconBubble:@"请输入职位名称"];
        }
        if (isOk && [self.workExperienceView.startTime.text isEqualToString:@"必填"]) {
            isOk = NO;
            [self showSingleIconBubble:@"请选择工作开始时间"];
        }
        if (isOk && [self.workExperienceView.endTime.text isEqualToString:@"必填"]) {
            isOk = NO;
            [self showSingleIconBubble:@"请输入工作结束时间"];
        }
        if (isOk && self.workExperienceView.desTextView.text.length == 0) {
            isOk = NO;
            [self showSingleIconBubble:@"请填写所在职位描述"];
        }
    }else{
        if (isOk && self.educationView.schoolNameTextField.text.length == 0) {
            isOk = NO;
            [self showSingleIconBubble:@"请输入学校名称"];
        }
        if (isOk && self.educationView.professionTextField.text.length == 0) {
            isOk = NO;
            [self showSingleIconBubble:@"请输入专业名称"];
        }
        if (isOk && [self.educationView.EducationalLabel.text isEqualToString:@"必填"]) {
            isOk = NO;
            [self showSingleIconBubble:@"请选择学历"];
        }
        if (isOk && [self.educationView.startTimeLabel.text isEqualToString:@"必填"]) {
            isOk = NO;
            [self showSingleIconBubble:@"请选择教育开始时间"];
        }
        if (isOk && [self.educationView.endTimeLabel.text isEqualToString:@"必填"]) {
            isOk = NO;
            [self showSingleIconBubble:@"请选择教育结束时间"];
        }
        if (isOk && self.educationView.EducationaTextView.text.length == 0) {
            isOk = NO;
            [self showSingleIconBubble:@"请填写所学专业描述"];
        }
    }
    if (isOk) {
        [self showRoundProgressWithTitle:@"请稍等..."];
        NSMutableDictionary *muDicParams = [NSMutableDictionary dictionary];
        muDicParams[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
        muDicParams[@"token"] = [USER_DEFAULTS objectForKey:@"token"];
        NSString *strUrl = @"";
        if ([self.tigStr integerValue] == 1) {//更改职业经历
            strUrl = @"updatePeopleWork_app_myHome.im.lensum";
            NSString *strTime = [NSString stringWithFormat:@"%@-%@",self.workExperienceView.startTime.text,self.workExperienceView.endTime.text];
            muDicParams[@"oldWorkName"] = self.oldExperienceDic[@"workName"];
            muDicParams[@"newWorkName"] = self.workExperienceView.companyTextField.text;
            muDicParams[@"workPositionName"] = self.workExperienceView.positionTextField.text;
            muDicParams[@"content"] = self.workExperienceView.desTextView.text;
            muDicParams[@"time"] = strTime;
            
        }else if ([self.tigStr integerValue] == 2){//更改教育经历
            strUrl = @"updatePeopleEducation_app_myHome.im.lensum";
            NSString *strTime = [NSString stringWithFormat:@"%@-%@",self.educationView.startTimeLabel.text,self.educationView.endTimeLabel.text];
            muDicParams[@"time"] = strTime;
            muDicParams[@"oldSchoolName"] = self.oldExperienceDic[@"schoolName"];
            muDicParams[@"newSchoolName"] = self.educationView.schoolNameTextField.text;
            muDicParams[@"schoolStudy"] = self.educationView.professionTextField.text;
            muDicParams[@"schoolIng"] = self.educationView.EducationalLabel.text;
            muDicParams[@"content"] = self.educationView.EducationaTextView.text;
        }else if ([self.tigStr integerValue] == 3){//添加职业经历
            strUrl = @"addPeopleWork_app_myHome.im.lensum";
            NSString *strTime = [NSString stringWithFormat:@"%@-%@",self.workExperienceView.startTime.text,self.workExperienceView.endTime.text];
            muDicParams[@"workName"] = self.workExperienceView.companyTextField.text;
            muDicParams[@"workPositionName"] = self.workExperienceView.positionTextField.text;
            muDicParams[@"content"] = self.workExperienceView.desTextView.text;
            muDicParams[@"time"] = strTime;
        }else{//添加教育经历
            strUrl = @"addPeopleEducation_app_myHome.im.lensum";
            NSString *strTime = [NSString stringWithFormat:@"%@-%@",self.educationView.startTimeLabel.text,self.educationView.endTimeLabel.text];
            muDicParams[@"time"] = strTime;
            muDicParams[@"schoolName"] = self.educationView.schoolNameTextField.text;
            muDicParams[@"schoolStudy"] = self.educationView.professionTextField.text;
            muDicParams[@"schoolIng"] = self.educationView.EducationalLabel.text;
            muDicParams[@"content"] = self.educationView.EducationaTextView.text;
        }
        [LSHttpTool sendHttpData:strUrl params:muDicParams success:^(id responseObject) {
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            if ([dicInfo[@"result"] integerValue] == 1) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"experienceVCExp" object:nil userInfo:dicInfo];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            [self hideBubble];
        } failure:^(NSError *error) {

            [self showSingleIconBubble:error.localizedDescription];

        }];

    }
}


- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
